package com.comp6231.frontend;

import java.io.FileNotFoundException;
import java.io.PrintWriter;

import org.omg.CORBA.ORB;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
import org.omg.PortableServer.POAPackage.ObjectNotActive;
import org.omg.PortableServer.POAPackage.ServantAlreadyActive;
import org.omg.PortableServer.POAPackage.WrongPolicy;

public class ClientInterface extends Thread {

	/**
	 * @param args
	 * @throws InvalidName
	 * @throws WrongPolicy
	 * @throws ServantAlreadyActive
	 * @throws ObjectNotActive
	 * @throws FileNotFoundException
	 * @throws AdapterInactive
	 */
	public void run() {

		try {
			// bind the server into orb object
			ORB orb = ORB.init(new String[] { "" }, null);
			POA rootPOA = POAHelper.narrow(orb
					.resolve_initial_references("RootPOA"));

			byte[] id;
			id = rootPOA.activate_object(new StationServerImplementation());

			org.omg.CORBA.Object ref = rootPOA.id_to_reference(id);

			String ior = orb.object_to_string(ref);
			PrintWriter file = new PrintWriter(System.getProperty("user.dir")
					+ "/src/com/comp6231/frontend/"
					+ "FEIor.txt");
			file.println(ior);
			file.close();

			rootPOA.the_POAManager().activate();

			System.out
					.println("CORBA Front End is ready to receive client request");

			orb.run();

		} catch (InvalidName | ServantAlreadyActive | WrongPolicy
				| ObjectNotActive | FileNotFoundException | AdapterInactive e) {
			e.printStackTrace();
		}

	}
}
